package com.emex.app.web.service;

import com.emex.app.shared.data.Cashbox;
import com.emex.app.shared.data.OrderPayment;
import com.emex.app.shared.data.OrderPaymentExample;
import com.emex.app.shared.persistance.CashboxMapper;
import com.emex.app.shared.persistance.OrderPaymentMapper;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Sergey Karavaev
 */
@Service
@Transactional
public class OrderPaymentService {

    @Autowired
    private OrderPaymentMapper orderPaymentMapper;
    @Autowired
    private CashboxMapper cashboxMapper;

    public int deleteOrderPaymentByPrimaryKey(Integer id) {
        return orderPaymentMapper.deleteByPrimaryKey(id);
    }

    public OrderPayment insertOrderPayment(OrderPayment record) {
        record.setDate(correctDate(record.getDate()));
        orderPaymentMapper.insert(record);
        return record;
    }

    public List<OrderPayment> selectOrderPaymentByExample(OrderPaymentExample example) {
        List<OrderPayment> list = orderPaymentMapper.selectByExample(example);
        correctDate(list);
        return list;
    }

    public OrderPayment selectOrderPaymentByPrimaryKey(Integer id) {
        OrderPayment res = orderPaymentMapper.selectByPrimaryKey(id);
        res.setDate(correctDate(res.getDate()));
        return res;
    }

    public OrderPayment updatOrderPaymentByPrimaryKey(OrderPayment record) {
        record.setDate(correctDate(record.getDate()));
        orderPaymentMapper.updateByPrimaryKey(record);
        return record;
    }

    public List<OrderPayment> getOrderPaymentByOrderId(Integer orderId) {
        OrderPaymentExample example = new OrderPaymentExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        List<OrderPayment> list = selectOrderPaymentByExample(example);
        correctDate(list);
        return list;
    }

    public List<OrderPayment> getOrderPaymentsByDate(Date startDate, Date endDate) {
        OrderPaymentExample example = new OrderPaymentExample();
        example.createCriteria().andDateBetween(correctDate(startDate), correctDate(endDate));
        List<OrderPayment> list = selectOrderPaymentByExample(example);
        correctDate(list);
        return list;
    }

    public List<OrderPayment> getOrderPaymentByClientId(Integer clientId) {
        OrderPaymentExample example = new OrderPaymentExample();
        example.createCriteria().andClientIdEqualTo(clientId);
        List<OrderPayment> list = selectOrderPaymentByExample(example);
        correctDate(list);
        return list;
    }

    public List<OrderPayment> getOrderPaymentsByDateEqualOrGrater(Date date) {
        OrderPaymentExample example = new OrderPaymentExample();
        example.createCriteria().andDateGreaterThanOrEqualTo(correctDate(date));
        List<OrderPayment> list = selectOrderPaymentByExample(example);
        correctDate(list);
        return list;
    }

    public Cashbox insertCashbox(Cashbox record) {
        record.setDate(correctDate(record.getDate()));
        cashboxMapper.insert(record);
        return record;
    }

    public Cashbox getCashBox() {
        List<Cashbox> cashbox = cashboxMapper.selectByExample(null);
        return cashbox.size() > 0 ? cashbox.get(0) : null;
    }

    private Date correctDate(Date op) {
        final long hours12 = 4L * 60L * 60L * 1000L;
        Date date = new Date(op.getTime() + hours12);
        return date;
    }

    private void correctDate(List<OrderPayment> list) {
        if (null != list && !list.isEmpty()) {
            for (OrderPayment record : list) {
                record.setDate(correctDate(record.getDate()));
            }
        }
    }
}
